import re


# 匹配ip
def ip_re(str):
    """
    简单匹配ip 忽略ip规则
    :param str:
    :return:
    """
    pattern = re.compile(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
    find_res = (pattern.findall(str))
    try:
        return find_res[0]
    except IndexError:
        return None


# 匹配端口号

def port_re(str):
    """
    先匹配有<td>80</td>规则的 然后把port提取出来即可
    :param str:
    :return:
    """
    pattern = re.compile(r"<td>\d+</td>")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        port = res[4:-5]

        return int(port)

    except IndexError:
        return None


def proxy_type_re(str):
    """
    匹配http方法
    <td>HTTPS</td>
    :param str:
    :return:
    """
    pattern = re.compile(r"<td>HTTPS</td>|<td>HTTP</td>")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        type = res[4:-5]

        return type

    except IndexError:
        return None

#https://movie.douban.com/celebrity/1010627/
def celebrity_re(str):
    """
    匹配职员的url链接
    :param str:
    :return:
    """
    pattern = re.compile(r"celebrity/\d+")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        return res
    except IndexError:
        return None

def movies_re(str):
    """
    匹配职员的url链接
    :param str:
    :return:
    """
    pattern = re.compile(r"subject/\d+")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        return res
    except IndexError:
        return None

def imdb_re(str):
    pattern = re.compile(r"https://www\.imdb\.com/title/\w*\d+/|http://www\.imdb\.com/title/\w*\d+")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        return res
    except IndexError:
        return None


def urlid_re(str):
    pattern = re.compile(r"\d+")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        return res
    except IndexError:
        return None

def avatar_re(str):
    #'background-image: url(https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403264395.93.jpg)'
    pattern = re.compile(r"https://.*\)|http://.*\)")
    find_res = (pattern.findall(str))
    try:
        res = find_res[0]
        return res[:-1]
    except IndexError:
        return None





if __name__ == "__main__":
    y = avatar_re("""
    
    <tr class="odd">
      <td class="country"><img src="//fs.xicidaili.com/images/flag/cn.png" alt="Cn"></td>
      <td>183.185.184.157</td>
      <td>8120</td>
      <td>
        <a href="/2019-03-31/shanxi">山西太原</a>
      </td>
      <td class="country">高匿</td>
      <td>HTTPS</td>
      <td class="country">
        <div title="0.05秒" class="bar">
          <div class="bar_inner fast" style="width:94%">
            
          </div>
        </div>
      </td>
      <td class="country">
        <div title="0.01秒" class="bar">
          <div class="bar_inner fast" style="width:97%">
            
          </div>
        </div>
      </td>
      
      <td>4小时</td>
      <td>19-03-31 09:00</td>
      <a href="background-image: url(https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403264395.93.jpg)">全部 15</a>
    </tr>
    """)

    print(y)
